package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_GHOST_GET_UNION_NEWS_FROM_CROSS, desc = "获取商会情报跨服返回")
public class GhostGetUnionNewsFromCrossCmd extends AbstractCommand {

    List<GhostActivityProto.GhostUnionNewsMsg> newsList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        if (packet.getBytes() != null) {
            GhostActivityProto.CrossGhostGetUnionNewsRespMsg reqMsg = GhostActivityProto.CrossGhostGetUnionNewsRespMsg.parseFrom(packet.getBytes());
            newsList = reqMsg.getNewsListList();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 添加个昵称
        List<GhostActivityProto.GhostUnionNewsMsg> list = new ArrayList<>();
        for (GhostActivityProto.GhostUnionNewsMsg newsMsg : newsList) {
            GhostActivityProto.GhostUnionNewsMsg.Builder builder = newsMsg.toBuilder();
            String nickName = "";
            CrossUnionMember unionMember = CrossUnionMgr.getUnionMember(builder.getUserId());
            if (unionMember != null) {
                UserBaseInfo userBaseInfo = unionMember.getUserBaseInfo();
                if (userBaseInfo != null) {
                    nickName = userBaseInfo.getNickName();
                }
            }
            builder.setNickName(nickName);
            list.add(builder.build());
        }

        GhostActivityProto.GetUnionNewsListRespMsg.Builder builder = GhostActivityProto.GetUnionNewsListRespMsg.newBuilder();
        builder.setRet(0);
        builder.addAllUnionNewsList(list);
        player.sendPacket(ClientProtocol.U_GHOST_GET_UNION_NEWS, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
